const db = require("../common/db/index");

module.exports = {
    // 获取轮播图详情
    detail(id) {
        return new Promise((resolve, reject) => {

            const sql = 'SELECT id, banner_title, banner_link, banner_url FROM pre_banner WHERE id = ?';

            db.query(sql, [Number(id)])
                .then(
                    result => {
                        resolve(result)
                    },
                    err => {
                        reject(err)
                    }
                )
        })
    },
    // 获取轮播图列表
    list() {
        return new Promise((resolve, reject) => {

            const sql = 'SELECT id, banner_title, banner_link, banner_url FROM pre_banner';

            db.query(sql)
                .then(
                    result => {
                        resolve(result)
                    },
                    err => {
                        reject(err)
                    }
                )

        })
    },
    // 搜索轮播图（按标题或链接模糊查询）
    search(keyword) {
        return new Promise((resolve, reject) => {
            // 同时搜索标题和链接
            const sql = 'SELECT * FROM pre_banner WHERE banner_title LIKE ? OR banner_link LIKE ?';
            db.query(sql, [`%${keyword}%`, `%${keyword}%`])
                .then(
                    result => {
                        resolve(result)
                    },
                    err => {
                        reject(err)
                    }
                )
        })
    },
    // 添加轮播图
    add(banner_title, banner_link, banner_url) {
        return new Promise((resolve, reject) => {

            const sql = 'INSERT INTO pre_banner (banner_title, banner_link, banner_url) VALUES (?, ?, ?)';
            db.query(sql, [banner_title, banner_link, banner_url])
                .then(
                    result => {
                        resolve(result)
                    },
                    err => {
                        reject(err)
                    }
                )

        })
    },
    // 修改轮播图
    modify(banner_title, banner_link, banner_url, id) {
        return new Promise((resolve, reject) => {

            const sql = 'UPDATE pre_banner SET banner_title = ?, banner_link = ? , banner_url = ? WHERE id = ?';
            db.query(sql, [banner_title, banner_link, banner_url, id])
                .then(
                    result => {
                        resolve(result)
                    },
                    err => {
                        reject(err)
                    }
                )

        })
    },
    // 删除轮播图
    remove(id) {
        return new Promise((resolve, reject) => {
            const sql = 'DELETE FROM pre_banner WHERE id = ?';
            db.query(sql, [id])
                .then(
                    result => {
                        resolve(result)
                    },
                    err => {
                        reject(err)
                    }
                )

        })
    },
    // 获取轮播图列表（分页）
    items(page_num, page_size) {
        return new Promise(async (resolve, reject) => {
            const rows = await db.query(`SELECT count(*) as total FROM pre_banner`);

            let page_num2 = ((parseInt(page_num) - 1) * parseInt(page_size))
            let page_size2 = (parseInt(page_size))
            let sql = `select * from pre_banner limit ${page_num2} , ${page_size2}`
            await db.query(sql)
                .then(
                    data => {
                        resolve({
                            total: rows[0].total,
                            data
                        })
                    },
                    err => {
                        reject(err)
                    }
                )

        })
    }
}